package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.QueryPageParam;
import com.campsys.common.Result;
import com.campsys.entity.Student;
import com.campsys.entity.Teacher;
import com.campsys.entity.dto.StudentDto;
import com.campsys.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping({"/student"})
public class StudentController{
    @Autowired
    private StudentService studentService;

    @PostMapping("/list")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        Page<Student> page =new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = (String) param.get("name");
        String isFirst = (String) param.get("isFirst");
        String isAudit = (String) param.get("isAudit");
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(Student::getStuName,name);
        }
        if (StringUtils.isNotBlank(isFirst)){
            lambdaQueryWrapper.eq(Student::getIsFirst,isFirst);
        }
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.eq(Student::getIsAudit,isAudit);
        }
        IPage result =studentService.page(page,lambdaQueryWrapper);
        return Result.suc(result.getTotal(),result.getRecords());
    }
    @PostMapping("/save")
    public Result createStudent(@RequestBody Student student){
        return studentService.createStudent(student)?Result.suc():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Student student){
        return studentService.updateById(student)?Result.suc():Result.fail();
    }
    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return studentService.removeById(id)?Result.suc():Result.fail();
    }
    @PostMapping("/import")
    public Result importStudents(@RequestBody List<StudentDto> students) {
       return studentService.importStudents(students)?Result.suc():Result.fail();
    }
}
